<script lang="ts" setup>
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { type FormInstance, type FormRules, ElMessage } from "element-plus"
import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue"
import { modifyPassword } from "@/api/login"

const router = useRouter()
const loginFormRef = ref<FormInstance | null>(null)
const loading = ref(false)
const loginFormData: any = reactive({
  username: "",
  oldPassword: "",
  newPassword: "",
  confirmPassword: "",
})
const validateNewPassword = (rule: any, value: any, callback: any) => {
  if (loginFormData.oldPassword) {
    if(loginFormData.oldPassword == loginFormData.newPassword) {
      callback(new Error('旧密码不能与新密码相同'))
      return
    }
  }
  callback()
}
const validateConfirmPassword = (rule: any, value: any, callback: any) => {
  if (loginFormData.newPassword) {
    if(loginFormData.confirmPassword != loginFormData.newPassword) {
      callback(new Error('新密码与确认新密码不一致'))
      return
    }
  }
  callback()
}
const loginFormRules: FormRules = {
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  oldPassword: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { validator: validateNewPassword, trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: "请输入确认新密码", trigger: "blur" },
    { validator: validateConfirmPassword, trigger: 'blur' },
  ],
}

const handleLogin = () => {
  loginFormRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return
    loading.value = true
    modifyPassword(loginFormData).then((res)=>{
      ElMessage({
        message: '操作成功',
        type: 'success',
      })
      setTimeout(()=> {
        router.push({path: '/login'})
      },2000)
    }).catch(()=>{
      loading.value = false
    })
  })
}

const back = () => {
  router.push({path: '/login'})
}
</script>

<template>
  <div class="login-container">
    <div class="login-card">
      <div class="content">
        <div class="editpassword-title">
          <el-icon @click="back"><Back /></el-icon>
          修改密码
        </div>
        <el-form ref="loginFormRef" :model="loginFormData" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="username">
            <el-input
              v-model.trim="loginFormData.username"
              placeholder="请输入账号"
              type="text"
              tabindex="1"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="oldPassword">
            <el-input
              v-model.trim="loginFormData.oldPassword"
              placeholder="请输入旧密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-form-item prop="newPassword">
            <el-input
              v-model.trim="loginFormData.newPassword"
              placeholder="请输入新密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input
              v-model.trim="loginFormData.confirmPassword"
              placeholder="请再输一遍新密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-button :loading="loading" type="primary" @click.prevent="handleLogin">保  存</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.handle{
  display: flex;
  justify-content: space-between;
  align-items: center;
  .editPassword{
    cursor: pointer;
    font-size:12px;
    color:var(--el-color-primary);
  }
  :deep(.el-checkbox__label){
    font-size:12px;
  }
}
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;
  .login-card {
    width: 428px;
    border-radius: 20px;
    box-shadow: 0 0 10px #dcdfe6;
    background-color: var(--el-bg-color);
    overflow: hidden;
    .editpassword-title{
      font-size:20px;
      font-weight: 600;
      display: flex;
      align-items: center;
      gap:16px;
      padding:30px 0 40px 0;
      .el-icon{
        cursor: pointer;
      }
    }
    .content {
      padding: 20px 50px 50px 50px;
      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 32px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }
      .el-button {
        width: 100%;
        margin-top: 10px;
      }
    }
  }
}
</style>
